package com.che.libcommon.ui.loading;

import android.os.Bundle;
import android.os.Looper;
import android.os.MessageQueue.IdleHandler;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import com.che.libcommon.ui.BaseActivity;
import com.che.libcommon.ui.VOMessage;

public abstract class LoadingActivity extends BaseActivity implements ILoadingView {

  private LoadingDelegate mLoadingDelegate;

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

  }

  @Override
  public void setContentView(int layoutResID) {
    super.setContentView(layoutResID);

    if(needLoading()) {
      mLoadingDelegate = new LoadingDelegate(findViewById(getRootLayoutViewId()),
          getLoadingFrameId(), getConfigure());
    }

  }

  @Override
  public void showLoading(boolean isShow) {
    if(mLoadingDelegate != null){
      mLoadingDelegate.showLoading(isShow);
    }
  }

  @Override
  public void showError(VOMessage voError) {
    if(mLoadingDelegate != null){
      mLoadingDelegate.showError(voError);
    }
  }

  @Override
  public void showData() {
    if(mLoadingDelegate != null){
      mLoadingDelegate.showData();
    }
  }

  protected void showError(Throwable throwable){
    showError(VOMessage.create(throwable));
  }


  protected abstract @IdRes int getRootLayoutViewId();

  protected abstract @IdRes int getLoadingFrameId();

  protected boolean needLoading(){
    return true;
  }

}
